<script setup>
import { useRouter } from 'vue-router';
import { GetUserInfo,uploadAvatar,upUserInfo } from '@/api/user'
import { ref } from "vue"
import { showToast } from 'vant';
import { onMounted, onUnmounted } from 'vue';



let errimg = new URL('../../../assets/img/erricon.png', import.meta.url).href ;

async function uploadUserAvatar(e){
  landingShow.value = true
  let inputDOM = e.target.files[0]
  let formData = new FormData();
  formData.append('file',inputDOM)
  let res = await uploadAvatar(formData)
  console.log(JSON.stringify(res))
  user_info.value.headIcon = res.data['data']['imgUrl']
  let upres = await upUserInfo(user_info.value)
  console.log(JSON.stringify(upres))
  landingShow.value = false
  if(upres.data.msg === 'ok'){
    showToast('保存成功');
  }else{
    showToast(`${upres.data.msg}`);
  }

}

let upUserInfoShow = ref(false)

let upTitle = ref('')
let editUserInfo = (title)=>{
  upUserInfoShow.value = true
  upTitle.value = title
}

let editColse = () => {
  console.log('66666')
  user_info.value.nickName   = uRes.nickName
  // upUserInfoShow.value = false

}
async function editUesrInfo(){
  landingShow.value = true
  console.log('ttttt')

  editData.value.nickName = upnickname.value
  let upres = await upUserInfo(user_info.value)
  // get_user_info()
  console.log(JSON.stringify(upres))
  landingShow.value = false
  if(upres.data.msg === 'ok'){
    showToast('保存成功');
  }else{
    showToast(`${upres.data.msg}`);
  }

}

let uRes = {}
let userfileList = ref([])



let landingShow = ref(false)

const router = useRouter();
let gotoBack = () => {

  router.go(-1)
  // window.location.reload(); // 刷新当前页面
  // showToast('返回成功')

}
let upnickname = ref('')
let editData = ref({
  nickName: '',
  headIcon: '',
  userId:'',
  gender: '',
  birthday: '',
  mobile:'',
})

let user_info = ref({
  nickName: '',
  headIcon: '',
  userId:'',
  gender: '',
  birthday: '',
  mobile:'',
})
async function get_user_info(token) {
  let params = `?k=7636500`
  let res = await GetUserInfo(params,token)
  uRes = res.data.data
  if(uRes){
    user_info.value   = uRes
    editData.value = uRes
    upnickname.value = uRes.nickName
    console.log(JSON.stringify(user_info.value))
    console.log(JSON.stringify(userfileList.value))
  }

}

const selectedValues = ref([]);

let selectedSexShow = ref(false)

let checked = ref(true)

function deletecache() {
  localStorage.clear()
  let cityid = localStorage.getItem("cityId")
  let cityname=  localStorage.getItem("cityName")
  if(cityid===null || cityname ===null){
    showToast('清除成功！');
  }
  console.log(`${cityid},${cityname}`)
}

async function onConfirmUp (){
  selectedSexShow.value = false;
  landingShow.value = true
  user_info.value.gender =  selectedValues.value[0]
  let upres = await upUserInfo(user_info.value)
  console.log(JSON.stringify(upres))
  landingShow.value = false
  if(upres.data.msg === 'ok'){
    showToast('保存成功');
  }else{
    showToast(`${upres.data.msg}`);
  }
}


let recommendChecked = ref(true)
let msgChecked = ref(true)

function deleteTokenCache() {
  window.localStorage.removeItem('token');
}
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function qiutLogin() {
  deleteTokenCache()
  await sleep(500);
  gotoBack()
  router.push({
    path: '/my'
  })
}




onMounted(() => {
  let token = localStorage.getItem("token")
  if (!!token){
    token=token.replace("\"","").replace("\"","");
  }
  get_user_info(token)
})

</script>

<template>
  <div class="my-body">
    <div class="top-box"></div>
      <div class="settings-header">
        <van-nav-bar class="settings-header-title" title="设置"  @click-left="gotoBack">
          <template #left>
            <van-icon name="arrow-left" size="20" color="#6f7070" />
          </template>
        </van-nav-bar>
      </div>



    <div  class="cell-card">
      <br/>
      <van-cell title="账号ID" :value="user_info.userId?user_info.userId:'未登录'" is-link to="" />
      <br/>
      <van-cell-group>
        <van-cell title="登录密码" value="修改"  is-link to="" />
        <van-cell title="安全密码" value="修改"  is-link to="" />
      </van-cell-group>

      <br/>
      <van-cell-group>
        <van-cell title="手机号码" :value="user_info.mobile?user_info.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'):'未登录'"  is-link to="" />
        <van-cell title="软件版本" value=""  is-link to="/VersionPage" />
        <van-cell title="隐私政策" value=""  is-link to="" />
        <van-cell title="用户协议" value=""  is-link to="" />
        <van-cell title="意见反馈" value=""  is-link to="" />
      </van-cell-group>

      <br/>
      <div style="background-color: #fff;width: 100%">
        <div style="background-color: #fff;width: 100%">
          <div class="switch-box1">
            <div class="switch-title-box">
              <div  class="switch-title-text">个性化推荐</div>
              <div class="switch-title-content">根据观影记录优先推荐你最近去过的影院</div>
            </div>
            <van-switch v-model="recommendChecked" size="20" active-color="#ee0a24" inactive-color="#dcdee0" />
          </div>

        </div>
        <div class="switch-box2">
          <div class="switch-title-box">
            <div class="switch-title-text">站内信消息推送</div>
            <div class="switch-title-content">购票成功或失败消息推送，建议打开</div>
          </div>
          <van-switch v-model="msgChecked" size="20" active-color="#ee0a24" inactive-color="#dcdee0" />
        </div>
      </div>
      <br/>
      <div style="display: flex;background-color: #fff; justify-content: space-between;align-items: center;height:35px;padding-left: 15px;padding-right: 15px;"  @click="deletecache()">
        <div  style="font-size: 13px;color: #323233;">清除缓存</div>
        <div style="font-size: 10px; color: #8d8a8a; padding-right: 10px">36M</div>
      </div>
      <br>

      <van-cell title="注销账号" value=""  is-link to="" />
      <br/><br/><br/>
      <div style="height: 60px;"></div>
      <van-sticky style="width: 100%" position="bottom" offset-bottom="0">

        <van-button class="quit-btn" color="#ff5f16" @click="qiutLogin()"  square>
          退出登录
        </van-button>
      </van-sticky>
    </div>






  </div>


</template>

<style scoped src="../css/UpdateUserInfo.css">


</style>

